package com.boatAnchorAssistant.service.repository.impl;

import com.boatAnchorAssistant.pojo.entity.Department;
import com.boatAnchorAssistant.pojo.exception.department.DepartmentNameRepeatException;
import com.boatAnchorAssistant.service.mapper.DepartmentMapper;
import com.boatAnchorAssistant.service.repository.in.DepartmentRepository;
import com.boatAnchorAssistant.service.utils.StringUtil;
import nirvana.core.repository.Repository;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.boatAnchorAssistant.pojo.dto.department.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@Service
public class DepartmentRepositoryImpl extends Repository<DepartmentMapper, Department> implements DepartmentRepository {

    private final static String TOP_DEP_ID = "TOP_DEP_ID_";

    private final DepartmentMapper mapper;

    private final RedisTemplate<String, Long> longRedisTemplate;

    public DepartmentRepositoryImpl(DepartmentMapper mapper, RedisTemplate<String, Long> longRedisTemplate) {
        this.mapper = mapper;
        this.longRedisTemplate = longRedisTemplate;
    }

    @Override
    public Department createToBefore(Department department) {
        String name = department.getName();
        Long parentId = department.getParentId();

        Department po=null;
        Department byPo=null;

        po= new Department();
        po.setName(name);
        po.setParentId(parentId);
        byPo=getByEntity(po);
        if (byPo!=null){
            throw new DepartmentNameRepeatException();
        }

        return super.createToBefore(department);
    }


    @Override
    public Department editToBefore(Department department) {
        String name = department.getName();
        Long parentId = department.getParentId();
        Long id = department.getId();

        Department po=null;
        Department byPo=null;

        po= new Department();
        po.setName(name);
        po.setParentId(parentId);
        byPo=getByEntity(po);
        if (byPo!=null&&!id.equals(byPo.getId())){
            throw new DepartmentNameRepeatException();
        }

        return super.editToBefore(department);
    }

    /**
     * 搜索部门
     * @param dto
     * @return
     */
    @Override
    public Page<Department> selectDepartment(SelectDepartmentDto dto) {

        return mapper.selectDepartment(dto);
    }

    /**
     * 获取部门列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<Department> getDepartmentListByDto(GetDepartmentListDto dto) {

        String name = dto.getName();
        if (!StringUtils.isEmpty(name)) {
            dto.setName(null);
        }

        List<Long> tenantIds = dto.getTenantIds();
        if (CollectionUtils.isEmpty(tenantIds)) {
            dto.setTenantIds(null);
        }
        return mapper.getDepartmentListByDto(dto);
    }

    /**
     * 批量删除企业
     *
     * @param idList id列表
     */
    @Override
    public void bathRemoveDepartment(List<Long> idList) {
        mapper.bathRemoveDepartment(idList);
    }

    /**
     * 获取企业顶级部门id
     *
     * @param tenantId
     * @return
     */
    @Override
    public Long getTopDepartmentId(Long tenantId) {
        ValueOperations<String, Long> ops = longRedisTemplate.opsForValue();
        String key = StringUtil.jointString(TOP_DEP_ID, tenantId.toString());

        Long topDepartmentId = ops.get(key);

        //缓存中没有值，则走db
        if (topDepartmentId != null) {
            topDepartmentId = mapper.getTopDepartmentId(tenantId);
            ops.set(key, topDepartmentId, 10, TimeUnit.MINUTES);
        }

        return topDepartmentId;
    }

    /**
     * 获取顶级部门
     *
     * @param tenantId 企业id
     * @return
     */
    @Override
    public Department getTopDepartment(Long tenantId) {
        Department po = new Department();
        po.setTenantId(tenantId);
        po.setLevel(0);
        return getByEntity(po);
    }

    /**
     * 根据企业id获取所有部门的id
     *
     * @param tenantId
     * @return
     */
    @Override
    public List<Long> getIdListByTenantId(Long tenantId) {
        return mapper.getIdListByTenantId(tenantId);
    }

}